<!--
 * @Author: your name
 * @Date: 2021-09-07 09:30:57
 * @LastEditTime: 2021-09-07 09:51:01
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \briup\5-javascript\day12\3-节点插入.html
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="parent">
        <div class="child">1</div>
        <div class="child">2</div>
        <div class="child">3</div>
        <div class="child">4</div>
    </div>
</body>
<script>
    var myDiv = document.createElement("div");
    myDiv.innerText = "x";
    var rootDiv = document.getElementsByClassName("parent")[0];
    var childNode = document.getElementsByClassName("child")[2];
    //myDiv插入到childNode前面
    rootDiv.insertBefore(myDiv, childNode);
</script>

</html>